package com.wxl.common.wiget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.wxl.common.R
import com.wxl.common.util.ImgLoader

/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */

class VTab : LinearLayout {

    var iconNormal: Int = 0
    var iconPress: Int = 0
    var textNormalColor: Int = R.color.text_gray_color
    var textPressColor: Int = R.color.text_black_color


    @SuppressLint("NewApi", "Recycle")
    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        attrs?.let {
            context.obtainStyledAttributes(attrs, R.styleable.VTab).also {
                val iconSize: Int = it.getDimensionPixelSize(R.styleable.VTab_icon_size, 40)
                iconNormal = it.getResourceId(R.styleable.VTab_normal_icon, 0)
                iconPress = it.getResourceId(R.styleable.VTab_press_icon, 0)
                val textSize: Float = it.getFloat(R.styleable.VTab_text_size, 12f)
                textNormalColor =
                    it.getColor(R.styleable.VTab_text_normal_color, ContextCompat.getColor(context,R.color.text_gray_color))
                textPressColor =
                    it.getColor(R.styleable.VTab_text_press_color, ContextCompat.getColor(context,R.color.text_black_color))
                val textMarginTop: Int =
                    it.getDimensionPixelSize(R.styleable.VTab_text_margin_top, 3)
                val text: String? = it.getString(R.styleable.VTab_text_str)

                load(
                    iconSize,
                    iconNormal,
                    iconPress,
                    textSize,
                    textNormalColor,
                    textPressColor,
                    textMarginTop,
                    text
                )
            }.recycle()
        }
    }


    /**
     * 家长icon
     */
    private fun loadImage(iconSize: Int, iconNormal: Int): ImageView {
        val icon: ImageView = createIcon(iconSize)
        ImgLoader.loadResouce(icon, iconNormal)
        return icon
    }


    /**
     * 创建一个icon控件
     */
    private fun createIcon(iconSize: Int): ImageView {
        val icon: ImageView = ImageView(context)
        icon.layoutParams = LayoutParams(iconSize, iconSize)
        return icon
    }


    /**
     * 加载textView
     */
    private fun loadText(textSize: Float, textNormalColor: Int, textMarginTop: Int,text: String?): TextView {
        val textView: TextView = createText(textMarginTop)
        textView.setTextColor(textNormalColor)
        textView.textSize = textSize
        textView.text = text
        return textView
    }


    /**
     * 创建textView
     */
    private fun createText(textMarginTop: Int): TextView {
        val textView: TextView = TextView(context)
        val params: LinearLayout.LayoutParams =
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        params.topMargin = textMarginTop
        textView.layoutParams = params
        return textView
    }

    /**
     * 加载view
     */
    private fun load(
        iconSize: Int,
        iconNormal: Int,
        iconPress: Int,
        textSize: Float,
        textNormalColor: Int,
        textPressColor: Int,
        textMarginTop: Int,
        text: String?
    ) {
        gravity = Gravity.CENTER
        orientation = VERTICAL
        val icon = loadImage(iconSize, iconNormal)
        val textView = loadText(textSize, textNormalColor, textMarginTop,text)
        addView(icon)
        addView(textView)
    }


    fun onClick(isPressed: Boolean) {
        val icon: ImageView = getChildAt(0) as ImageView
        val text: TextView = getChildAt(1) as TextView
        ImgLoader.loadResouce(icon,if(isPressed) iconPress else iconNormal)
        text.setTextColor(if(isPressed) textPressColor else textNormalColor)
    }
}

